# 共用的自定义工具类
from flask import current_app
from flask import session
import functools
from info.models import User
from flask import g

def do_index_class(index):
    """返回指定引索对应的类名"""
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"

    return ""


def user_login_data(func):
    # 使用 functools.wraps 去装饰内层函数，可以保持当前装饰器去装
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            # 尝试查询用户的模型
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 把查询出来的数据赋值给g变量
        g.user = user
        return func(*args, **kwargs)
    return wrapper


# def query_user_data():
#     user_id = session.get("user_id",None)
#     user = None    # 定义一个临时变量
#     if user_id:
#         # 尝试查询用户的模型
#         try:
#             user = User.query.get(user_id)
#         except Exception as e:
#             current_app.logger.error(e)
#         return user
#     return None